package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.LoginLogMapper;
import com.gxa.p2p.mapper.SysUserMapper;
import com.gxa.p2p.pojo.LoginLog;
import com.gxa.p2p.pojo.SysUser;
import com.gxa.p2p.service.SysUserService;
import com.gxa.p2p.util.MD5Creater;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @author Longlin_Feng
 * @date 2020/8/2
 */
@Service("sysService")
public class SysUserServiceImpl implements SysUserService {

  @Resource(name = "sysUserMapper")
  private SysUserMapper sysUserMapper;

  @Resource private LoginLogMapper loginLogMapper;

  /**
   * 添加系统用户
   *
   * @param sysUser 封装的系统用户对象
   */
  @Override
  @Transactional(rollbackFor = {Throwable.class})
  public boolean add(SysUser sysUser) {

    // 系统用户密码加密处理:密码明文+工号
    System.out.println(sysUser.getPassword());
    System.out.println(sysUser.getJobNumber());
    System.out.println(MD5Creater.encode(sysUser.getPassword() + sysUser.getJobNumber()));
    sysUser.setPassword(MD5Creater.encode(sysUser.getPassword() + sysUser.getJobNumber()));

    return sysUserMapper.insert(sysUser) > 0;
  }

  /**
   * 根据用户id删除
   *
   * @param id 要删除用户的id
   */
  @Override
  public boolean remove(Integer id) {

    return sysUserMapper.deleteByPrimaryKey(id) > 0;
  }

  /**
   * 根据id批量删除系统用户
   *
   * @param idList 要删除的用户id列表
   * @return boolean 批量删除成功或失败
   */
  @Override
  public boolean removeByBatch(List<Integer> idList) {

    return sysUserMapper.deleteByPrimaryKeyList(idList) == idList.size();
  }

  /**
   * 修改系统用防护信息
   *
   * @param sysUser 目标信息封装的用户对象
   * @return boolean 修改成功或失败
   */
  @Override
  public boolean edit(SysUser sysUser) {
    return sysUserMapper.updateByPrimaryKeySelective(sysUser) > 0;
  }

  /**
   * 分页查询
   *
   * @param paramMap 参数映射表:包含page,limit,和可能有的查询条件
   * @return List<SysUser>
   */
  @Override
  public List<SysUser> findByPage(Map<String, Object> paramMap) {

    // 处理分页页码:(page-1)*limit
    Integer page = ((Integer) paramMap.get("page") - 1) * (Integer) paramMap.get("limit");
    paramMap.put("page", page);
    // 模糊查询字段处理
    if (paramMap.get("name") != null) {
      paramMap.put("name", "%" + paramMap.get("name") + "%");
    } else if (paramMap.get("jobNumber") != null) {
      paramMap.put("jobNumber", "%" + paramMap.get("jobNumber") + "%");
    } else if (paramMap.get("phoneNumber") != null) {
      paramMap.put("phoneNumber", "%" + paramMap.get("phoneNumber") + "%");
    } else if (paramMap.get("gender") != null) {
      paramMap.put("gender", "%" + paramMap.get("gender") + "%");
    }

    return sysUserMapper.selectByPage(paramMap);
  }

  /**
   * 登录操作
   *
   * @param jobNumber 工号
   * @param password 密码
   * @param remoteAddr 发起请求的ip
   * @return boolean 成功或失败
   */
  @Override
  @Transactional(
      propagation = Propagation.REQUIRED,
      isolation = Isolation.READ_COMMITTED,
      rollbackFor = {Throwable.class})
  public SysUser login(String jobNumber, String password, String remoteAddr) {

    // 登录验证
    SysUser sysUser =
        sysUserMapper.selectByJobNumberAndPassword(
            jobNumber, MD5Creater.encode(password + jobNumber));
    System.out.println("password: " + password);
    System.out.println("密码::" + MD5Creater.encode(password + jobNumber));
    if (sysUser != null) {
      // 记录登录日志
      LoginLog loginLog = new LoginLog();
      loginLog.setSysUserId(sysUser.getId());
      // ip
      loginLog.setClientIp(remoteAddr);
      loginLogMapper.insertSelective(loginLog);
      return sysUser;
    }
    return null;
  }

  /**
   * 按条件全查询
   *
   * @param paramMap 参数映射表
   * @return List<SysUser>
   */
  @Override
  public List<SysUser> findAll(Map<String, Object> paramMap) {

    return sysUserMapper.selectAll(paramMap);
  }

  /**
   * 职务变更
   *
   * @param sysUser 职务变更后的目标对象
   * @return boolean 成功/失败
   */
  @Override
  public boolean positionChange(SysUser sysUser) {

    return sysUserMapper.updateRole(sysUser) > 0;
  }

  /**
   * 计数
   *
   * @param paramMap 封装了计数条件的map
   * @return count 总数
   */
  @Override
  public Integer getCount(Map<String, Object> paramMap) {
    return sysUserMapper.count(paramMap);
  }
}
